//////////////////////////////////////////////////////////////////////////

#ifndef _RESEARCHDIALOG_H_
#include "researchdialog.h"
#endif

//////////////////////////////////////////////////////////////////////////

#include "ui_researchdialog.h"

#ifndef _RESEARCH_H_
#include "research.h"
#endif

#ifndef _PATIENT_H_
#include "patient.h"
#endif

#include <QPrinter>
#include <QPrintDialog>
#include <QPainter>

//////////////////////////////////////////////////////////////////////////


ResearchDialog::ResearchDialog( QWidget* _parent )
: Dialog(_parent),
  m_ui(new Ui::ResearchDialog)
{
    m_ui->setupUi(this);

    // Setup search button
    connect( m_ui->m_PrintButton, SIGNAL(clicked()), this, SLOT(Print()) );
}


//////////////////////////////////////////////////////////////////////////


ResearchDialog::~ResearchDialog()
{
    delete m_ui;
}


//////////////////////////////////////////////////////////////////////////


void ResearchDialog::SetCurrResearch( const Research* _research )
{
    m_ui->m_ResearchDateEdit->setText( _research->GetDate() );

    const ResultsTable& resultsTable = _research->GetResultsTable();

    m_ui->m_TableWidget->setSpan(0, 0, 1, 2);
    m_ui->m_TableWidget->setSpan(1, 0, 1, 2);
    m_ui->m_TableWidget->setSpan(0, 2, 1, 2);
    m_ui->m_TableWidget->setSpan(1, 2, 1, 2);
    m_ui->m_TableWidget->setSpan(2, 0, 1, 4);
    m_ui->m_TableWidget->setSpan(8, 0, 1, 2);

    // Get step time in seconds
    char stepTime[10];
    sprintf( stepTime, "%.2f", resultsTable.m_StepTime / 1000.0 );

    m_ui->m_TableWidget->setItem(0, 2, new QTableWidgetItem( tr(stepTime) ));
    m_ui->m_TableWidget->setItem(1, 2, new QTableWidgetItem( tr("%1").arg(resultsTable.m_RythmCoefficient) ));
    m_ui->m_TableWidget->setItem(4, 1, new QTableWidgetItem( tr("%1").arg(resultsTable.m_RightHeelDown) ));
    m_ui->m_TableWidget->setItem(5, 1, new QTableWidgetItem( tr("%1").arg(resultsTable.m_RightFootDown) ));
    m_ui->m_TableWidget->setItem(6, 1, new QTableWidgetItem( tr("%1").arg(resultsTable.m_RightToeDown) ));
    m_ui->m_TableWidget->setItem(7, 1, new QTableWidgetItem( tr("%1").arg(resultsTable.m_RightFootUp) ));
    m_ui->m_TableWidget->setItem(4, 2, new QTableWidgetItem( tr("%1").arg(resultsTable.m_LeftHeelDown) ));
    m_ui->m_TableWidget->setItem(5, 2, new QTableWidgetItem( tr("%1").arg(resultsTable.m_LeftFootDown) ));
    m_ui->m_TableWidget->setItem(6, 2, new QTableWidgetItem( tr("%1").arg(resultsTable.m_LeftToeDown) ));
    m_ui->m_TableWidget->setItem(7, 2, new QTableWidgetItem( tr("%1").arg(resultsTable.m_LeftFootUp) ));
    m_ui->m_TableWidget->setItem(8, 2, new QTableWidgetItem( tr("%1").arg(resultsTable.m_DoubleSeatPhase) ));


    m_ui->m_TableWidget->item(4, 1)->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
    m_ui->m_TableWidget->item(5, 1)->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
    m_ui->m_TableWidget->item(6, 1)->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
    m_ui->m_TableWidget->item(7, 1)->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
    m_ui->m_TableWidget->item(4, 2)->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
    m_ui->m_TableWidget->item(5, 2)->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
    m_ui->m_TableWidget->item(6, 2)->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
    m_ui->m_TableWidget->item(7, 2)->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
    m_ui->m_TableWidget->item(8, 1)->setTextAlignment(Qt::AlignVCenter | Qt::AlignHCenter);


    int parentWidth = 300;
    int rightHeelWidth = (resultsTable.m_RightHeelDown / resultsTable.m_StepTime) * (double)parentWidth;
    int rightFootWidth = (resultsTable.m_RightFootDown / resultsTable.m_StepTime) * (double)parentWidth;
    int rightToeWidth = (resultsTable.m_RightToeDown / resultsTable.m_StepTime) * (double)parentWidth;
    int rightUpWidth = (resultsTable.m_RightFootUp / resultsTable.m_StepTime) * (double)parentWidth;

    int leftHeelWidth = (resultsTable.m_LeftHeelDown / resultsTable.m_StepTime) * (double)parentWidth;
    int leftFootWidth = (resultsTable.m_LeftFootDown / resultsTable.m_StepTime) * (double)parentWidth;
    int leftToeWidth = (resultsTable.m_LeftToeDown / resultsTable.m_StepTime) * (double)parentWidth;
    int leftUpWidth = (resultsTable.m_LeftFootUp / resultsTable.m_StepTime) * (double)parentWidth;

    

    m_ui->m_RightHeelWidget->setFixedWidth(rightHeelWidth);
    m_ui->m_RightFootWidget->setFixedWidth(rightFootWidth);
    m_ui->m_RightToeWidget->setFixedWidth(rightToeWidth);
    m_ui->m_RightUpWidget->setFixedWidth(rightUpWidth);

    m_ui->m_LeftHeelWidget->setFixedWidth(leftHeelWidth);
    m_ui->m_LeftFootWidget->setFixedWidth(leftFootWidth);
    m_ui->m_LeftToeWidget->setFixedWidth(leftToeWidth);
    m_ui->m_LeftUpWidget->setFixedWidth(leftUpWidth);
}


//////////////////////////////////////////////////////////////////////////


void ResearchDialog::Print()
{
    QPrinter* printer = new QPrinter;
    QPrintDialog* printDialog = new QPrintDialog(printer, this);
    if (printDialog->exec() == QDialog::Accepted)
    {
        QPainter p(printer);
        QRect rect(0, 0, 800, 423);

        QPalette palette;
        palette.setBrush(QPalette::Window,  QBrush(Qt::white) );
        this->setPalette(palette);

        QPalette heel = m_ui->m_RightHeelWidget->palette();
        QPalette foot = m_ui->m_RightFootWidget->palette();
        QPalette toe = m_ui->m_RightToeWidget->palette();
        QPalette up = m_ui->m_RightUpWidget->palette();

        palette.setBrush(QPalette::Window, Qt::BDiagPattern);
        m_ui->m_RightHeelWidget->setPalette(palette);
        m_ui->m_LeftHeelWidget->setPalette(palette);
        m_ui->m_TableWidget->item(4,3)->setBackground(palette.brush(QPalette::Window));

        palette.setBrush(QPalette::Window, Qt::CrossPattern);
        m_ui->m_RightFootWidget->setPalette(palette);
        m_ui->m_LeftFootWidget->setPalette(palette);
        m_ui->m_TableWidget->item(5,3)->setBackground(palette.brush(QPalette::Window));

        palette.setBrush(QPalette::Window, Qt::FDiagPattern);
        m_ui->m_RightToeWidget->setPalette(palette);
        m_ui->m_LeftToeWidget->setPalette(palette);
        m_ui->m_TableWidget->item(6,3)->setBackground(palette.brush(QPalette::Window));

        palette.setBrush(QPalette::Window, Qt::DiagCrossPattern);
        m_ui->m_RightUpWidget->setPalette(palette);
        m_ui->m_LeftUpWidget->setPalette(palette);
        m_ui->m_TableWidget->item(7,3)->setBackground(palette.brush(QPalette::Window));

        QPixmap pm = QPixmap::grabWidget(this, rect);

        this->setPalette( QPalette() );

        m_ui->m_RightHeelWidget->setPalette(heel);
        m_ui->m_LeftHeelWidget->setPalette(heel);
        m_ui->m_TableWidget->item(4,3)->setBackground(heel.brush(QPalette::Window));

        m_ui->m_RightFootWidget->setPalette(foot);
        m_ui->m_LeftFootWidget->setPalette(foot);
        m_ui->m_TableWidget->item(5,3)->setBackground(foot.brush(QPalette::Window));

        m_ui->m_RightToeWidget->setPalette(toe);
        m_ui->m_LeftToeWidget->setPalette(toe);
        m_ui->m_TableWidget->item(6,3)->setBackground(toe.brush(QPalette::Window));

        m_ui->m_RightUpWidget->setPalette(up);
        m_ui->m_LeftUpWidget->setPalette(up);
        m_ui->m_TableWidget->item(7,3)->setBackground(up.brush(QPalette::Window));

        p.drawPixmap(0, 0, pm);
    }
}


//////////////////////////////////////////////////////////////////////////


void ResearchDialog::SetCurrPatient( const Patient* _patient )
{
    m_ui->m_HistoryLineEdit->setText( QString::number( _patient->GetHistoryNumber() ) );
    m_ui->m_SurnameLineEdit->setText( _patient->GetSurname() );
    m_ui->m_NameLineEdit->setText( _patient->GetName() );
    m_ui->m_SecondnameLineEdit->setText( _patient->GetSecondName() );

    m_ui->m_BirhDateEdit->setText( _patient->GetBirthDate() );
    m_ui->m_GenderEdit->setText( ( _patient->GetSex() == Human::MALE ? QString(tr("Male")) : QString(tr("Female")) ) );

    m_ui->m_DiagnosisTextEdit->document()->setPlainText( _patient->GetDiagnosis() );
    m_ui->m_NotesTextEdit->document()->setPlainText( _patient->GetNotes() );
}


//////////////////////////////////////////////////////////////////////////
